<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\system;


use app\admin\model\SystemRegion;
use app\admin\service\TriggerService;
use app\common\constants\AdminConstant;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\App;

/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="签到管理")
 */
class Region extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'id' => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemRegion();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $pidMenuList = $this->model->getPidMenuList();
        $this->assign('pidMenuList', $pidMenuList);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $pidMenuList = $this->model->getPidMenuList();
        $this->assign([
            'id'          => $id,
            'pidMenuList' => $pidMenuList,
            'row'         => $row,
        ]);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="导入")
     */
    public function import()
    {
        if ($this->request->isPost()) {
            try {
                $file = $this->request->file('file');
            } catch (\Exception $e) {
                return '<script type="text/javascript">
                        alert("请上传文件")
                        history.go(-1)
                    </script>';
            }
            $ext = $file->getOriginalExtension();
            if(!in_array( $ext, ['xlsx'])){
                return '<script type="text/javascript">
                        alert("请上传xlsx文件")
                        history.go(-1)
                    </script>';
            }
            $savename = \think\facade\Filesystem::disk('public')->putFile( 'upload', $file);
            $path = rtrim(__FILE__,'app\admin\controller\system\Sign.php').'\public\\'.str_replace( '/','\\',$savename);
//            $path = rtrim(__FILE__,'app/admin/controller/system/Sign.php').'/public/'.$savename;
            //var_dump($path);die;
            $excel = IOFactory::load($path);
            $sheet = $excel->getSheet(0)->toArray();
            array_shift($sheet);  //删除第一个数组(标题);
            $data = [];
            foreach ($sheet as $k => $v) {
                $data[$k]['pid']=$v[1];
                $data[$k]['name']=$v[2];
                $data[$k]['level']=$v[3];
            }
            $save = $this->model->saveAll($data);
            if($save){
                return '<script type="text/javascript">
                        alert("导入成功")
                        parent.location.reload();
                    </script>';
            }
        }
       return  $this->fetch();
    }
    public function getParent($id){
        $parent = $this->model->find($id)->toArray();
        return json($parent);
    }
}